import 'dotenv/config';
import http from 'http';
const PORT = process.env.PORT;

const server = http.createServer((req, res) => {
    try{
        //检查是否为GET请求
        if (req.method === 'GET') {
            if(req.url === '/'){
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.end('<h1>主页</h1>');
            } else if (req.url === '/about') {
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.end('<h1>关于</h1>');
            } else{
                res.writeHead(404, {'Content-Type': 'text/html'});
                res.end('<h1>404 无法找到此页</h1>');
            }
        } else {
            throw new Error('非法请求');
        }
    } catch (error) {
        res.writeHead(500,{'Content-Type': 'text/html'});
        res.end(`<h1>${error.message}</h1>`);
    }  
});

server.listen(PORT, () => {
    console.log(`服务器已启动，监听端口${PORT}`);
});